home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Snippets / Up⁄Down Arrows / ArrowTester.c next >
Encoding:
C/C++ Source or Header  |  1994-03-04  |  10.0 KB  |  372 lines  |  [TEXT/KAHL]

  1. /*
  2.  * UpDownArrowTester.c
  3.  *
  4.  * By Eddy J. Gurney
  5.  *
  6.  * Please send any improvements or bugs in the "heart" of this code
  7.  * (the part that actually does the up/down arrow thing) to
  8.  * <egurney@vcd.hp.com>.  I don't care about bugs in the "shell" 
  9.  * portion; it's just a quick hack used to test the up/down arrow thing!
  10.  *
  11.  * If you find this code useful, a "thanks" in the About box of your
  12.  * app or an e-mail message would always be appreciated. :-)
  13.  *
  14.  */
  15.  
  16. #include <Palettes.h>
  17.  
  18. #define NIL                    0L
  19.  
  20. typedef enum DialogMode {
  21.     Default,         /* "Control" keys only */
  22.     AllowAlpha,
  23.     AllowPrint,
  24.     AllowDigit,
  25.     AllowHexDigit,
  26.     AllowAlphaNum    
  27. } DialogMode;
  28.  
  29. /* ASCII codes */
  30. #define enterKey            0x03
  31. #define returnKey            0x0d
  32. #define escapeKey            0x1b
  33.  
  34. DialogMode    gDialogMode;
  35. Boolean        gOkActive;
  36.  
  37. void ToolBoxInit()
  38. {
  39.     InitGraf(&thePort);
  40.     InitFonts();
  41.     FlushEvents(everyEvent, 0);
  42.     InitWindows();
  43.     InitMenus();
  44.     TEInit();
  45.     InitDialogs(NIL);
  46.     MaxApplZone();
  47.     InitCursor();
  48.     
  49.     SetDAFont(applFont);
  50. }
  51.  
  52. void FrameDialogItem(DialogPtr theDialog, short theItem, Pattern thePattern)
  53. {
  54.     GrafPtr            savePort;
  55.     short            iType;
  56.     Handle            iHandle;
  57.     Rect            iRect;
  58.     PenState        savePen;
  59.     RGBColor        color;
  60.     PixPatHandle    pp;
  61.  
  62.     GetPort(&savePort);
  63.     SetPort(theDialog);
  64.     GetPenState(&savePen);
  65.  
  66.     if (thePattern == gray) {
  67.         color.red = 0x8000;
  68.         color.green = 0x8000;
  69.         color.blue = 0x8000;
  70.     } else if (thePattern == black) {
  71.         color.red = 0x0;
  72.         color.green = 0x0;
  73.         color.blue = 0x0;
  74.     }    
  75.     pp = NewPixPat();
  76.     MakeRGBPat(pp, &color);
  77.     PenPixPat(pp);
  78.     
  79.     GetDItem(theDialog, theItem, &iType, &iHandle, &iRect);
  80.     InsetRect(&iRect, -4, -4);
  81.     PenSize(3, 3);
  82. /*    PenPat(thePattern);    */
  83.     FrameRoundRect(&iRect, 16, 16);
  84.  
  85.     DisposPixPat(pp);
  86.     SetPenState(&savePen);
  87.     SetPort(savePort);
  88. }
  89.  
  90. void DoDialogUpdate(DialogPtr theDialog)
  91. {
  92.     GrafPtr    savePort;
  93.     
  94.     GetPort(&savePort);
  95.     SetPort(theDialog);
  96.     
  97.     BeginUpdate(theDialog);
  98.     UpdtDialog(theDialog, theDialog->visRgn);
  99.     FrameDialogItem(theDialog, ok, (gOkActive ? black : gray));
  100.     EndUpdate(theDialog);
  101.     SetPort(savePort);
  102. }
  103.  
  104. void DoDialogActivate(DialogPtr theDialog, Boolean active)
  105. {
  106.     SetPort(theDialog);
  107. }
  108.  
  109. pascal Boolean DialogFilter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  110. {
  111.     short            iType;
  112.     Handle            iHandle;
  113.     Rect            iRect;
  114.     char            theChar;
  115.     long            endTicks;
  116.     Boolean            result = FALSE;
  117.     WindowPtr        theWindow;
  118.  
  119.     Point            mousePoint;
  120.     Rect            upArrowRect, downArrowRect, plainArrowRect;
  121.     Boolean            hiLit = FALSE;
  122.     Str255            theString;
  123.     long            theValue, theDelay;
  124.     PicHandle        upArrowPict, downArrowPict, plainArrowPict;
  125.  
  126.     theWindow = (WindowPtr)(theEvent->message);
  127.     switch (theEvent->what) {
  128.         case updateEvt:
  129.             if (theWindow == theDialog) {
  130.                 DoDialogUpdate(theDialog);
  131.                 result = TRUE;
  132.                 *theItem = 0;
  133.             } else {
  134.                 /* Call main DoUpdate(w) routine here to prevent holes in
  135.                    background windows (see TN 304) */
  136.             }
  137.             break;
  138.         case activateEvt:
  139.             if (theWindow == theDialog) {
  140.                 DoDialogActivate(theDialog, (theEvent->modifiers & activeFlag) != 0);
  141.                 *theItem = 0;
  142.             } else {
  143.                 /* Call main DoActivate(w) routine here to deactivate old
  144.                    frontmost window to unhighlight scroll bars, etc. */
  145.             }
  146.             break;
  147.         case mouseDown:
  148.             plainArrowPict = GetPicture(400); /* Get handles to all possible arrows */
  149.             upArrowPict = GetPicture(401);
  150.             downArrowPict = GetPicture(402);
  151.             
  152.             mousePoint = theEvent->where;
  153.             GlobalToLocal(&mousePoint);        /* Find out where mouse was clicked */
  154.             
  155.             GetDItem(theDialog, 5, &iType, &iHandle, &iRect);    /* Get loc. of arrow */
  156.             plainArrowRect = iRect;
  157.             upArrowRect = iRect;
  158.             upArrowRect.bottom -= 9;    /* Define rect for up arrow */
  159.             downArrowRect = iRect;
  160.             downArrowRect.top += 9;        /* Define rect for down arrow */
  161.             
  162.             endTicks = TickCount() + 15;    /* Calculate time til first auto-count */
  163.  
  164.             GetDItem(theDialog, 4, &iType, &iHandle, &iRect);    /* Edit text item */
  165.             HLock(iHandle);
  166.             GetIText(iHandle, theString);
  167.             StringToNum(theString, &theValue);    /* Convert to number */
  168.             
  169.             if (PtInRect(mousePoint, &upArrowRect)) {    /* Was click in up arrow? */
  170.                 DrawPicture(upArrowPict, &plainArrowRect);
  171.                 theDelay = 15;        /* Set initial delay for auto-increment */
  172.                 hiLit = TRUE;
  173.                 theValue++;        /* Increment it */
  174.                 NumToString(theValue, theString);    /* Convert to string */
  175.                 SetIText(iHandle, theString);        /* Display it */
  176.                 
  177.                 while (StillDown()) {    /* Track control if user still holding button */
  178.                     GetMouse(&mousePoint);
  179.                     if (PtInRect(mousePoint, &upArrowRect)) {    /* In up arrow? */
  180.                         if (hiLit == FALSE) {    /* If so and not highlighted, do it */
  181.                             hiLit = TRUE;
  182.                             DrawPicture(upArrowPict, &plainArrowRect);
  183.                         }
  184.                     } else {    /* Out of up arrow now, unhighlight and reset delay */
  185.                         if (hiLit == TRUE) {
  186.                             theDelay = 15;
  187.                             hiLit = FALSE;
  188.                             DrawPicture(plainArrowPict, &plainArrowRect);
  189.                         }
  190.                     }
  191.                     /* If enough time has passed, auto-increment value */
  192.                     if (hiLit == TRUE && TickCount() >= endTicks) {
  193.                         theValue++;        /* Increment value */
  194.                         NumToString(theValue, theString);    /* Convert to string */
  195.                         SetIText(iHandle, theString);    /* Display it */
  196.                         SelIText(theDialog, 4, 0, 32767);    /* Select the whole thing */
  197.                         endTicks = TickCount() + theDelay;    /* Calc new delay time */
  198.                         if (theDelay)
  199.                             theDelay--;        /* Shorten delay time until it's zero */
  200.                     }
  201.                 } /* end "while StillDown()" */
  202.             } /* end "click in up arrow" */
  203.             
  204.             if (PtInRect(mousePoint, &downArrowRect)) {    /* Was click in down arrow? */
  205.                 DrawPicture(downArrowPict, &plainArrowRect);
  206.                 theDelay = 15;
  207.                 hiLit = TRUE;
  208.                 theValue--;        /* Decrement it */
  209.                 NumToString(theValue, theString);    /* Convert to string */
  210.                 SetIText(iHandle, theString);        /* Display it */
  211.                 
  212.                 while (StillDown()) {    /* Track control if user still holding button */
  213.                     GetMouse(&mousePoint);
  214.                     if (PtInRect(mousePoint, &downArrowRect)) {        /* In down arrow? */
  215.                         if (hiLit == FALSE) {    /* If so and not highlighted, do it */
  216.                             hiLit = TRUE;
  217.                             DrawPicture(downArrowPict, &plainArrowRect);
  218.                         }
  219.                     } else {    /* Out of down arrow now, unhighlight and reset delay */
  220.                         if (hiLit == TRUE) {
  221.                             theDelay = 15;
  222.                             hiLit = FALSE;
  223.                             DrawPicture(plainArrowPict, &plainArrowRect);
  224.                         }
  225.                     }
  226.                     /* If enough time has passed, auto-decrement value */
  227.                     if (hiLit == TRUE && TickCount() >= endTicks) {
  228.                         theValue--;        /* Increment value */
  229.                         NumToString(theValue, theString);    /* Convert to string */
  230.                         SetIText(iHandle, theString);    /* Display it */
  231.                         SelIText(theDialog, 4, 0, 32767);    /* Select the whole thing */
  232.                         endTicks = TickCount() + theDelay;    /* Calc new delay time */
  233.                         if (theDelay)
  234.                             theDelay--;        /* Shorten delay time until it's zero */
  235.                     }
  236.                 } /* end "while StillDown()" */
  237.             } /* end "click in down arrow" */
  238.             
  239.             HUnlock(iHandle);
  240.             SelIText(theDialog, 4, 0, 32767);  /* Select the whole thing */
  241.  
  242.             if (hiLit == TRUE) {    /* Unhighlight arrow if necessary */
  243.                 DrawPicture(plainArrowPict, &plainArrowRect);
  244.                 *theItem = 5;
  245.                 result = TRUE;
  246.             }
  247.             
  248.             ReleaseResource((Handle)plainArrowPict);    /* Release arrow PICTs */
  249.             ReleaseResource((Handle)upArrowPict);
  250.             ReleaseResource((Handle)downArrowPict);
  251.             break;
  252.         case mouseUp:
  253.             break;
  254.         case keyDown:
  255.         case autoKey:
  256.             theChar = theEvent->message & charCodeMask;
  257.             if (theChar == returnKey || theChar == enterKey) {    /* Return/Enter key? */
  258.                 if (gOkActive) {      /* Might be able to use contrlHilite field instead? */
  259.                     GetDItem(theDialog, ok, &iType, &iHandle, &iRect);
  260.                     HiliteControl((ControlHandle)iHandle, 1);
  261.                     Delay(8, &endTicks);
  262.                     HiliteControl((ControlHandle)iHandle, 0);
  263.                     *theItem = ok;
  264.                     return TRUE;
  265.                 } else {
  266.                     *theItem = 0;
  267.                     return TRUE;
  268.                 }
  269.             } else if ((theChar == '.' && theEvent->modifiers & cmdKey) 
  270.                         || theChar == escapeKey) {        /* ESC or Cmd-.? */
  271.                 GetDItem(theDialog, cancel, &iType, &iHandle, &iRect);
  272.                 HiliteControl((ControlHandle)iHandle, 1);
  273.                 Delay(8, &endTicks);
  274.                 HiliteControl((ControlHandle)iHandle, 0);
  275.                 *theItem = cancel;
  276.                 return TRUE;
  277.             } else {
  278. #ifdef HAS_ISxxxx    /* The "isxxxxx()" functions are easily implemented with a
  279.                        256-byte lookup table... see "ctype.h"/"ctype.c" in Think C.
  280.                        Then you can trap out illegal keys.  Without them, all keys
  281.                        are valid... */
  282.                 switch (gDialogMode) {
  283.                     case AllowAlpha:
  284.                         result = isalpha(theChar);
  285.                         break;
  286.                     case AllowPrint:
  287.                         result = isprint(theChar);
  288.                         break;
  289.                     case AllowDigit:
  290.                         result = isdigit(theChar);
  291.                         break;
  292.                     case AllowHexDigit:
  293.                         result = isxdigit(theChar);
  294.                         break;
  295.                     case AllowAlphaNum:
  296.                         result = isalnum(theChar);
  297.                         break;
  298.                     default:
  299.                         result = FALSE;
  300.                 }
  301.                 result |= iscntrl(theChar);        /* Always allow spec. keys */
  302. #else
  303.                 result = TRUE;
  304. #endif
  305.                 if (result)
  306.                     return FALSE;
  307.                 else {
  308.                     SysBeep(1);
  309.                     *theItem = 0;
  310.                     return TRUE;
  311.                 }
  312.             }
  313.             break;
  314.     }
  315.     return result;
  316. }
  317.  
  318. main()
  319. {
  320.     GrafPtr            savePort;
  321.     DialogPtr        theDialog;
  322.     short            itemHit, iType;
  323.     Handle            iHandle, rsrcHandle;
  324.     Boolean            dialogDone = FALSE;
  325.     Rect            iRect;
  326.     Str255            theString;
  327.     long            theValue;
  328.     
  329.     ToolBoxInit();
  330.     
  331.     if ((theDialog = GetNewDialog(400, NIL, (WindowPtr)-1)) == NIL) {
  332.         SysBeep(1);
  333.         ExitToShell();
  334.     }
  335.     
  336.     GetPort(&savePort);    
  337.     SelectWindow(theDialog);
  338.       SetPort(theDialog);
  339.  
  340.     ShowWindow(theDialog);
  341.     SelIText(theDialog, 4, 0, 32767);
  342.     
  343.     gOkActive = FALSE;
  344.     gDialogMode = AllowDigit;
  345.     while (!dialogDone) {
  346.         GetDItem(theDialog, ok, &iType, &iHandle, &iRect);
  347.         HiliteControl((ControlHandle)iHandle, (gOkActive ? 0 : 255));
  348.         FrameDialogItem(theDialog, ok, (gOkActive ? black : gray));
  349.         ModalDialog(DialogFilter, &itemHit);
  350.         switch (itemHit) {
  351.             case ok:
  352.                 dialogDone = TRUE;
  353.                 break;
  354.             case cancel:
  355.                 dialogDone = TRUE;
  356.                 break;
  357.             case 4:        /* Edit text item */
  358.             case 5:        /* Pict item of small up/down arrows */
  359.                 GetDItem(theDialog, 4, &iType, &iHandle, &iRect);
  360.                 GetIText(iHandle, theString);
  361.                 StringToNum(theString, &theValue);
  362.                 if (theValue > 9)
  363.                     gOkActive = TRUE;
  364.                 else
  365.                     gOkActive = FALSE;
  366.                 break;
  367.         }
  368.     }
  369.     DisposDialog(theDialog);
  370.       SetPort(savePort);
  371. }
  372.